package com.goktech.controller;

import com.goktech.entity.Book;
import com.goktech.entity.User;
import com.goktech.entity.collect;
import com.goktech.service.CollectService;
import com.goktech.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName userCollectController
 * @Description TODO
 * @Author 邱华英
 * @Date 2022/10/9 23:13
 * @Version 1.0
 */

@Controller
@RequestMapping("/user/collect")
public class userCollectController {

    @Autowired
    private CollectService collectService;

    @Autowired
    private UserService userService;

    @Autowired
    private User u;

    @Autowired
    private Book b;

    @Autowired
    private collect c;

    @RequestMapping("/")
    public String CollectSelectAll(){
        return "redirect:search/add?yeshu=0";
    }

    @RequestMapping("/search/add")
    public String searchAdd(Integer yeshu, HttpServletRequest request){
        HttpSession session = request.getSession();

        String zhanghao = (String) session.getAttribute("zhanghao");
        u.setZhanghao(zhanghao);
        User byUser = userService.findByUser(u);

        List<Map<String, Object>> maps = collectService.selectAll(byUser, yeshu * 5, 5);

        if(maps.size() == 0 && yeshu!=0){
            maps = collectService.selectAll(byUser,(yeshu-1)*5,5);
        }else{
            yeshu=yeshu+1;
        }
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("books",maps);
        System.out.println("yeshu:"+yeshu);
        return "user/collect";
    }

    @RequestMapping("/search/delete")
    public String searchDelete(Integer yeshu,HttpServletRequest request){
        HttpSession session = request.getSession();

        String zhanghao = (String) session.getAttribute("zhanghao");
        u.setZhanghao(zhanghao);
        User byUser = userService.findByUser(u);

        List<Map<String, Object>> maps  = null;
        if(yeshu>1){
            yeshu=yeshu-1;
        }
        maps = collectService.selectAll(byUser,(yeshu-1)*5,5);
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("books",maps);
        return "user/collect";
    }

    @PostMapping("/select")
    public String collectSelect(@Param("select") String select, @Param("text") String text, HttpServletRequest request) throws UnsupportedEncodingException {
        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        u.setZhanghao(zhanghao);

        User byUser = userService.findByUser(u);
        String text1 = new String(text.getBytes("ISO-8859-1"), "utf-8");

        List<Map<String, Object>> byLike = null;
        if("bookName".equals(select)){
            b.setBName("%"+text1+"%");
            System.out.println("text1:"+text1);
            byLike = collectService.findByLikeBookName(byUser, b);
        }else if("author".equals(select)){
            b.setBAuthor("%"+text1+"%");
            byLike = collectService.findByLikeAuthor(byUser,b);
        }
        System.out.println("byLike:"+byLike);
        request.setAttribute("yeshu",1);
        request.setAttribute("books",byLike);
        return "user/collect";
    }

    @RequestMapping ("/delete")
    public String delete(@Param("bookid")String bookid,HttpServletRequest request){
        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        u.setZhanghao(zhanghao);
        User byUser = userService.findByUser(u);

        c.setUserId(byUser.getId());
        c.setBookId(Integer.parseInt(bookid));

        int i = collectService.deleteCollect(c);

        return "redirect:search/add?yeshu=0";
    }
}
